package com.yxy.service;

import com.yxy.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class FileUploadService {
    @Autowired
    private FileUploadUtil fileUploadUtil;

    public Map<String, Object>  uploadFile(MultipartFile file){

        Map<String, Object> map= new HashMap<>();
        map.put("name",file.getOriginalFilename());
        String uuid = fileUploadUtil.uploadFile(file);
        map.put("uuid",uuid);
        return  map;
    }

    public List<Map<String, Object>>  uploadFileList(List<MultipartFile> fileList){

        Map<String, Object> map= new HashMap<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        for(int i = 0;i<fileList.size();i++){
            MultipartFile file = fileList.get(i);
            map.put("name",file.getOriginalFilename());
            String uuid = fileUploadUtil.uploadFile(file);
            map.put("uuid",uuid);
            resultList.add(map);
        }

        return  resultList;
    }

    public List<Map<String, Object>>  uploadFileList(MultipartFile[] fileList){
        List<Map<String, Object>> resultList  = new ArrayList<>();
        for(MultipartFile file:fileList){
            Map<String, Object> map= new HashMap<>();
            map.put("name",file.getOriginalFilename());
            String uuid = fileUploadUtil.uploadFile(file);
            map.put("uuid",uuid);
            resultList.add(map);
        }
        return resultList;
    }


    public void  downloadFile(HttpServletResponse response, String fileName){

        fileUploadUtil.downloadFile(response,fileName);

    }
    public void  downloadFiles(HttpServletResponse response, String fileName){

        fileUploadUtil.downloadFiles(response,fileName);

    }

    public void  downloadPdfFile(HttpServletResponse response, String fileName){

        fileUploadUtil.downloadPdfFile(response,fileName);

    }


}
